package org.jeecg.modules.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.CommonCuckoo;
import org.jeecg.cuckoo.enums.TransactionStatusEnum;
import org.jeecg.cuckoo.enums.TransactionTypeEnum;
import org.jeecg.modules.entity.CuCommissionManagement;
import org.jeecg.modules.entity.CuUserDetails;
import org.jeecg.modules.entity.CuWalletaMnagement;
import org.jeecg.modules.entity.CuWalletaTransaction;
import org.jeecg.modules.mapper.CuUserDetailsMapper;
import org.jeecg.modules.mapper.CuWalletaMnagementMapper;
import org.jeecg.modules.mapper.CuWalletaTransactionMapper;
import org.jeecg.modules.service.ICuCommissionManagementService;
import org.jeecg.modules.service.ICuUserDetailsService;
import org.jeecg.modules.service.ICuWalletaMnagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Description: 钱包管理
 * @Author: jeecg-boot
 * @Date: 2024-08-05
 * @Version: V1.0
 */
@Service
public class CuWalletaMnagementServiceImpl extends ServiceImpl<CuWalletaMnagementMapper, CuWalletaMnagement> implements ICuWalletaMnagementService {

    @Autowired
    private ICuUserDetailsService cuUserDetailsService;

    @Autowired
    private CuWalletaTransactionMapper walletaTransactionMapper;

    @Autowired
    private ICuCommissionManagementService commissionManagementService;

    @Override
    public CuWalletaMnagement queryByUserId(CuUserDetails userDetails) {
        CuWalletaMnagement walletaMnagement = new CuWalletaMnagement();
        walletaMnagement.setUserId(userDetails.getId());
        QueryWrapper<CuWalletaMnagement> queryWrapper = new QueryWrapper<>(walletaMnagement);
        walletaMnagement = super.getOne(queryWrapper);
        if (ObjectUtil.isEmpty(walletaMnagement)) {
            walletaMnagement = new CuWalletaMnagement();
            walletaMnagement.setUserId(userDetails.getId());
            walletaMnagement.setUserName(userDetails.getUserName());
            walletaMnagement.setBalance(BigDecimal.valueOf(0));
            walletaMnagement.setStatus(CommonCuckoo.STATUS_INT_1);
            super.save(walletaMnagement);
        }
        return walletaMnagement;
    }

    @Override
    public boolean updateWalletAmount(String userId, BigDecimal amount, String type, String start, String description, String otherId) {
        // 获取用户信息
        CuUserDetails userDetails = cuUserDetailsService.getById(userId);
        // 获取用户钱包
        CuWalletaMnagement walletaMnagement = queryByUserId(userDetails);
        // 修改钱包余额 type： "1" 收入  "2" 支出  "3" 不操作余额
        if (TransactionTypeEnum.TRANSACTION_TYPE_INCOME.equals(type)) {
            // 加
            walletaMnagement.setBalance(walletaMnagement.getBalance().add(amount));
        } else if (TransactionTypeEnum.TRANSACTION_TYPE_OUTCOME.equals(type)) {
            // 减 判断金额小于扣去的金额
            if (walletaMnagement.getBalance().compareTo(amount) < 0) {
                return false;
            }
            walletaMnagement.setBalance(walletaMnagement.getBalance().subtract(amount));
        }

        // 记录信息
        CuWalletaTransaction walletaTransaction = new CuWalletaTransaction();
        walletaTransaction.setTransactionId(walletaMnagement.getId());
        walletaTransaction.setAccountId(userDetails.getId());
        walletaTransaction.setAmount(amount);
        walletaTransaction.setTransactionType(type);
        walletaTransaction.setDescription(description);
        walletaTransaction.setStatus(start);
        walletaTransaction.setOtherId(otherId);
        walletaTransaction.setBalance(walletaMnagement.getBalance());

        // 保存
        super.updateById(walletaMnagement);
        walletaTransactionMapper.insert(walletaTransaction);

        return true;
    }

    @Override
    public boolean payReward(CuUserDetails userDetails, BigDecimal amount) {
        // 先拿到分成列表
        List<CuCommissionManagement> commissionManagementList = commissionManagementService.getAll();
        Integer length = commissionManagementList.size();

        // 获取上级 给分成
        List<CuUserDetails> parentList = cuUserDetailsService.getParentList(userDetails.getRegisterCode(), length);
        if (ObjectUtil.isEmpty(parentList)) {
            return false;
        }
        for (int i = 0; i < parentList.size(); i++) {
            CuUserDetails user = parentList.get(i);
            CuCommissionManagement commissionManagement = commissionManagementList.get(i);
            // 判断返点规则  固定 还是 百分比
            BigDecimal subtract = BigDecimal.valueOf(0);
            if (commissionManagement.getType().equals(CommonCuckoo.RETURN_CENT)) {
                // 提成 = 总金额 * （提成 / 100）%
                subtract = amount.multiply(commissionManagement.getValue().divide(BigDecimal.valueOf(100)));
            } else if (commissionManagement.getType().equals(CommonCuckoo.RETURN_REGULAR)) {
                // 提成 = 固定提成金额
                subtract = commissionManagement.getValue();
            }

            // 入库记录 修改钱包
            String description = "获取提成";
            // String userId, BigDecimal amount, String type, Integer start, String description, String otherId
            updateWalletAmount(
                    user.getId(),
                    subtract,
                    TransactionTypeEnum.TRANSACTION_TYPE_INCOME,
                    TransactionStatusEnum.TRANSACTION_STATUS_CONFIRMED,
                    description,
                    userDetails.getId()
            );
        }

        return false;
    }
}
